package com.example.androidapp2;

import java.util.Calendar;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;

public class PopUpConfigu extends Dialog  {
	DbAdapter DbConect;
	Context Ctx;

	public EditText txtSueldo;
	public CheckBox chbTarjeta;
	public DatePicker dtpCierre;
	public DatePicker dtpVencimiento;
	public Button btnGuardar;

	public PopUpConfigu(Context context, DbAdapter db) {
		super(context);
		Ctx = context;
		DbConect = db;
		this.setContentView(R.layout.pup_config);		

		txtSueldo = (EditText) findViewById(R.id.txtSueldo);
		chbTarjeta = (CheckBox) findViewById(R.id.chbTarjeta);
		dtpCierre = (DatePicker) findViewById(R.id.dtpCierre);
		dtpVencimiento = (DatePicker) findViewById(R.id.dtpVencimiento);
		btnGuardar = (Button) findViewById(R.id.btnGuardar);

		dtpCierre.setEnabled(false);
		dtpVencimiento.setEnabled(false);
		SetListenerBotonesPopUpTarjetas();
	}

	public void SetListenerBotonesPopUpTarjetas()
	{		
		chbTarjeta.setOnCheckedChangeListener(new OnCheckedChangeListener() {			
			public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
				HabilitarControlesTarjeta();				
			}
		});

		btnGuardar.setOnClickListener(new View.OnClickListener() {			
			public void onClick(View v) {
				if(txtSueldo.getText().toString().length()>0){
					DbConect.InsertMovimiento(Integer.parseInt(txtSueldo.getText().toString()), "In", GetFecha(), "EFT", "0");
					((MainActivity)Ctx).ActualizarProgressBar();
					hide();					
				}
			}
		});
	}

	public void HabilitarControlesTarjeta()
	{
		if(chbTarjeta.isChecked())
		{
			dtpCierre.setEnabled(true);
			dtpVencimiento.setEnabled(true);			
		}else
		{
			dtpCierre.setEnabled(false);
			dtpVencimiento.setEnabled(false);
		}		
	}

	private String GetFecha()
	{
		Calendar c = Calendar.getInstance(); 
		
		String fecha = (
				((c.get(Calendar.DATE)<10)?"0"+String.valueOf( c.get(Calendar.DATE)):String.valueOf( c.get(Calendar.DATE)))+
				((c.get(Calendar.MONTH)+1<10)?"0"+String.valueOf( c.get(Calendar.MONTH)+1):String.valueOf( c.get(Calendar.MONTH)+1))+
				((c.get(Calendar.YEAR)<10)?"0"+String.valueOf( c.get(Calendar.YEAR)):String.valueOf( c.get(Calendar.YEAR)))+
				((c.get(Calendar.HOUR_OF_DAY)<10)?"0"+String.valueOf( c.get(Calendar.HOUR_OF_DAY)):String.valueOf( c.get(Calendar.HOUR_OF_DAY)))+
				((c.get(Calendar.MINUTE)<10)?"0"+String.valueOf( c.get(Calendar.MINUTE)):String.valueOf( c.get(Calendar.MINUTE)))
				);

		return fecha;
	}
}
